<?php

namespace Biz\service\GoodsTestActivity;

use Biz\model\GoodsTestActivity\FineGoodsModel;
use Biz\service\BrandRights\BrandRightsAllService;

class PackageService
{
    /**
     * @param $package
     * @param $num
     * @return array
     */
    public static function getPackageConf($package, $num): array
    {
        $dataConf = FineGoodsModel::PACKAGE_FANS_NUM[$package];
        $result = [];
        $condition = [];
        $i = 1;
        foreach ($dataConf as $v) {
            $val = [];
            $value = [];
            $val['key'] = $i;
            $val['name'] = FineGoodsModel::PACKAGE[$package];
            $val['num'] = $num;
            $val['fans_num'] = $v;
            list($one, $two, $three, $four, $five, $fifteen) = self::getNum($package, $num, $v);
            $v = array_search($v, FineGoodsModel::PACKAGE_FANS_GRADE_MAP);
            if ($one > 0) {
                $value[$v] = $one;
            }
            if ($two > 0) {
                $value[$v] = $two;
            }
            if ($three > 0) {
                $value[$v] = $three;
            }
            if ($four > 0) {
                $value[$v] = $four;
            }
            if ($five > 0) {
                $value[$v] = $five;
            }
            if ($fifteen > 0) {
                $value[$v] = $fifteen;
            }

            if ($package == FineGoodsModel::PACKAGE_BUSINESS_COOPERATION) {
                $val['fifteen'] = $fifteen;
            } else {
                $val['one'] = $one;
                $val['two'] = $two;
                $val['three'] = $three;
                $val['four'] = $four;
                $val['five'] = $five;
            }
            $result['list'] [] = $val;
            if ($value) {
                $condition [] = $value;
            }
            $i++;
        }

        return [$result, $condition];
    }

    /**
     * @param $package
     * @param $num
     * @param $fansNum
     * @return int[]
     */
    public static function getNum($package, $num, $fansNum): array
    {
        $one = 0;
        $two = 0;
        $three = 0;
        $four = 0;
        $five = 0;
        $fifteen = 0;
        switch ($package) {
            case FineGoodsModel::PACKAGE_A:
                if ($num == 1 && $fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_A][0]) {
                    $one = 1;
                }
                if ($num == 2 && $fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_A][1]) {
                    $two = 2;
                }
                if ($num == 3) {
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_A][1]) {
                        $three = 1;
                    }
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_A][2]) {
                        $three = 1;
                    }
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_A][3]) {
                        $three = 1;
                    }
                }
                if ($num == 4) {
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_A][2]) {
                        $four = 2;
                    }
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_A][3]) {
                        $four = 2;
                    }
                }
                if ($num == 5) {
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_A][1]) {
                        $five = 1;
                    }
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_A][3]) {
                        $five = 4;
                    }
                }
                break;
            case FineGoodsModel::PACKAGE_B:
                if ($num == 1 && $fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_B][0]) {
                    $one = 1;
                }
                if ($num == 2 && $fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_B][1]) {
                    $two = 2;
                }
                if ($num == 3) {
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_B][1]) {
                        $three = 1;
                    }
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_B][2]) {
                        $three = 2;
                    }
                }
                if ($num == 4) {
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_B][1]) {
                        $four = 1;
                    }
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_B][2]) {
                        $four = 1;
                    }
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_B][3]) {
                        $four = 1;
                    }
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_B][4]) {
                        $four = 1;
                    }
                }
                if ($num == 5) {
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_B][1]) {
                        $five = 1;
                    }
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_B][2]) {
                        $five = 1;
                    }
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_B][3]) {
                        $five = 1;
                    }
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_B][4]) {
                        $five = 2;
                    }
                }
                break;
            case FineGoodsModel::PACKAGE_C:
                if ($num == 2) {
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_C][0]) {
                        $two = 1;
                    }
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_C][1]) {
                        $two = 1;
                    }
                }

                if ($num == 3) {
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_C][0]) {
                        $three = 1;
                    }
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_C][2]) {
                        $three = 2;
                    }
                }
                if ($num == 4) {
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_C][1]) {
                        $four = 1;
                    }
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_C][2]) {
                        $four = 3;
                    }
                }
                if ($num == 5) {
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_C][1]) {
                        $five = 2;
                    }
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_C][2]) {
                        $five = 1;
                    }
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_C][3]) {
                        $five = 1;
                    }
                    if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_C][4]) {
                        $five = 1;
                    }
                }
                break;
            case FineGoodsModel::PACKAGE_BUSINESS_COOPERATION:
                if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_BUSINESS_COOPERATION][0]) {
                    $fifteen = 2;
                }
                if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_BUSINESS_COOPERATION][1]) {
                    $fifteen = 4;
                }
                if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_BUSINESS_COOPERATION][2]) {
                    $fifteen = 4;
                }
                if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_BUSINESS_COOPERATION][3]) {
                    $fifteen = 2;
                }
                if ($fansNum == FineGoodsModel::PACKAGE_FANS_NUM[FineGoodsModel::PACKAGE_BUSINESS_COOPERATION][4]) {
                    $fifteen = 3;
                }
            default:
        }

        return [$one, $two, $three, $four, $five, $fifteen];
    }

    /**
     * 获取套餐列表数据
     * @param int $brandId
     * @return array[]
     */
    public static function getPackageList(int $brandId): array
    {
        $packageNumMap = BrandRightsAllService::getGoodTestPackNum($brandId);
        $packageANum = $packageNumMap[FineGoodsModel::PACKAGE_A] ?? 0;
        $packageBNum = $packageNumMap[FineGoodsModel::PACKAGE_B] ?? 0;
        $packageCNum = $packageNumMap[FineGoodsModel::PACKAGE_C] ?? 0;
        $packageBusinessCooperationNum = $packageNumMap[FineGoodsModel::PACKAGE_BUSINESS_COOPERATION] ?? 0;
        $packageBusinessCooperation [] = [
            'name' => FineGoodsModel::PACKAGE[FineGoodsModel::PACKAGE_BUSINESS_COOPERATION],
            'package' => FineGoodsModel::PACKAGE_BUSINESS_COOPERATION,
            'fans_num' => '3000',//最大粉丝数
            'price' => '45000',//原价
            'rebate_price' => '24000',//折扣价
            'remaining_times' => $packageBusinessCooperationNum,
            'remaining_times_msg' => '剩余'.$packageBusinessCooperationNum.'次',
            'test_num' => '15',//可参与测评用户数量
        ];


        $packageList = [
            [
                'name' => FineGoodsModel::PACKAGE[FineGoodsModel::PACKAGE_A],
                'package' => FineGoodsModel::PACKAGE_A,
                'fans_num' => '2000',//最大粉丝数
                'price' => '2200',//原价
                'rebate_price' => '2000',//折扣价
                'remaining_times' => $packageANum,
                'remaining_times_msg' => '剩余'.$packageANum.'次',
                'test_num' => '1-5',//可参与测评用户数量
            ],
            [
                'name' => FineGoodsModel::PACKAGE[FineGoodsModel::PACKAGE_B],
                'package' => FineGoodsModel::PACKAGE_B,
                'fans_num' => '3000',
                'price' => '4500',
                'rebate_price' => '4000',
                'remaining_times' => $packageBNum,
                'remaining_times_msg' => '剩余'.$packageBNum.'次',
                'test_num' => '1-5',
            ],
            [
                'name' => FineGoodsModel::PACKAGE[FineGoodsModel::PACKAGE_C],
                'package' => FineGoodsModel::PACKAGE_C,
                'fans_num' => '3000',
                'price' => '7000',
                'rebate_price' => '6000',
                'remaining_times' => $packageCNum,
                'remaining_times_msg' => '剩余'.$packageCNum.'次',
                'test_num' => '2-5',
            ],
        ];
        if ($packageBusinessCooperationNum > 0) {
            return array_merge($packageBusinessCooperation, $packageList);
        }

        return $packageList;
    }

}